package calculator.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

import layouts.StuLayout;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import javax.swing.JLabel;
import java.awt.Font;


public class CalculatorPanel extends JPanel {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	ExpressionPanel expressionPanel = null;

	@SuppressWarnings("unused")
	private	String input	= "";
	private String output	= "";
	/**
	 * Create the panel.
	 */
	public CalculatorPanel() {
		setLayout(new BorderLayout(0, 0));

		JPanel MainPanel = new JPanel();
		MainPanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		add(MainPanel, BorderLayout.CENTER);
		MainPanel.setLayout(new BorderLayout(0, 0));

		JPanel allButtonsPanel = new JPanel();
		allButtonsPanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
				JButton button_equals		= new JButton("=");
				button_equals.addActionListener(	new EvaluateButtonListener());
						allButtonsPanel.setLayout(new GridLayout(6, 4, 0, 0));
						JButton button_open			= new JButton("(");
						button_open.addActionListener(		new ButtonListener(button_open));
						allButtonsPanel.add(button_open);
						JButton button_close		= new JButton(")");
						button_close.addActionListener(		new ButtonListener(button_close));
						allButtonsPanel.add(button_close);
						JButton button_clear		= new JButton("CLR");
						button_clear.addActionListener(		new ClearButtonListener());
						JButton buttonn_delete		= new JButton("DEL");
						buttonn_delete.addActionListener(	new DeleteButtonListener());
						allButtonsPanel.add(buttonn_delete);
						allButtonsPanel.add(button_clear);
						JButton button_roll			= new JButton("ROLL");
						button_roll.addActionListener(		new ButtonListener(button_roll));
						allButtonsPanel.add(button_roll);
						JButton buttton_modulus		= new JButton("MOD");
						buttton_modulus.addActionListener(	new ButtonListener(buttton_modulus));
						allButtonsPanel.add(buttton_modulus);
						JButton button_power		= new JButton("POW");
						button_power.addActionListener(		new ButtonListener(button_power));
						allButtonsPanel.add(button_power);
						JButton button_add			= new JButton("+");
						button_add.addActionListener(		new ButtonListener(button_add));
						allButtonsPanel.add(button_add);
						JButton button_7 = new JButton("7");
						button_7.addActionListener(new ButtonListener(button_7));
						allButtonsPanel.add(button_7);
						JButton button_8 = new JButton("8");
						button_8.addActionListener(new ButtonListener(button_8));
						allButtonsPanel.add(button_8);
						JButton button_9 = new JButton("9");
						button_9.addActionListener(new ButtonListener(button_9));
						allButtonsPanel.add(button_9);
						JButton button_subtract		= new JButton("-");
						button_subtract.addActionListener(	new ButtonListener(button_subtract));
						allButtonsPanel.add(button_subtract);
						JButton button_4 = new JButton("4");
						button_4.addActionListener(new ButtonListener(button_4));
						allButtonsPanel.add(button_4);
						JButton button_5 = new JButton("5");
						button_5.addActionListener(new ButtonListener(button_5));
						allButtonsPanel.add(button_5);
						JButton button_6 = new JButton("6");
						button_6.addActionListener(new ButtonListener(button_6));
						allButtonsPanel.add(button_6);
								JButton button_multiply		= new JButton("*");
								button_multiply.addActionListener(	new ButtonListener(button_multiply));
								allButtonsPanel.add(button_multiply);

								//
								// Operands
								//

								JButton button_1 = new JButton("1");

										button_1.addActionListener(new ButtonListener(button_1));
										allButtonsPanel.add(button_1);
						JButton button_2 = new JButton("2");
						button_2.addActionListener(new ButtonListener(button_2));
						allButtonsPanel.add(button_2);
						JButton button_3 = new JButton("3");
						button_3.addActionListener(new ButtonListener(button_3));
						allButtonsPanel.add(button_3);
						JButton button_divide		= new JButton("/");
						button_divide.addActionListener(	new ButtonListener(button_divide));
						allButtonsPanel.add(button_divide);

						//
						// Operators
						//

						JButton button_decimal		= new JButton(".");

								button_decimal.addActionListener(	new ButtonListener(button_decimal));
										allButtonsPanel.add(button_decimal);
				JButton button_0			= new JButton("0");
				button_0.addActionListener(			new ButtonListener(button_0));
				allButtonsPanel.add(button_0);
				allButtonsPanel.add(button_equals);


		MainPanel.add(allButtonsPanel, BorderLayout.CENTER);

		expressionPanel = new ExpressionPanel();
		expressionPanel.getScreenTextField().setFont(new Font("Arial", Font.BOLD, 34));
		expressionPanel.setButtonVisible(false);
		MainPanel.add(expressionPanel, BorderLayout.NORTH);



	}


	private class ButtonListener implements ActionListener {

		private final JButton button;

		public ButtonListener(JButton button) {
			this.button = button;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			addText(button.getText());
		}

	}

	private class EvaluateButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			getExpressionPanel().evaluate();
		}

	}

	private class DeleteButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			String	S = getExpressionPanel().getScreenText();
			if (S==null) S = "";
			if (S.length()>0) {
				S = S.substring(0, S.length()-1);
			}
			getExpressionPanel().getScreenTextField().setText(S);
		}

	}

	private class ClearButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			getExpressionPanel().getScreenTextField().setText("");
		}

	}

	//
	// Internal methods
	//

	private void addText(String text) {
		//
		// The one method will parse and text coming in
		//
		String	input = getInput().toUpperCase();
		if (text.equals("MOD"))							{	text = "%";	}
		if (text.equals("ROLL"))						{	text = "d";	}
		if (text.equals("POW"))							{	text = "^";	}

		setInput(input + text);
	}

	public String getInput() {
		return getExpressionPanel().getScreenTextField().getText();
	}

	public void setInput(String input) {
		getExpressionPanel().getScreenTextField().setText(input);
	}

	public String getOutput() {
		getExpressionPanel().evaluate();
		return getExpressionPanel().getScreenTextField().getText();
	}

	public ExpressionPanel getExpressionPanel() {
		return expressionPanel;
	}

	public void setOutput(String output) {
		this.output = output;
	}
}
